package com.my.vote.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.my.vote.entity.VoteOption;
import com.my.vote.repository.VoteOptionRepository;
import com.sskj.core.annotation.ControllerLogExeTime;

@RestController
@RequestMapping("/vote/option")
public class VoteOptionServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	VoteOptionRepository voteOptionRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/one", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询全部")
	public VoteOption findOne(String id) {
		return voteOptionRepository.findOne(id);
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/all", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询全部")
	public List<VoteOption> findAllByQuestionId(String questionId) {
		List<VoteOption> result = voteOptionRepository.findAllByQuestionId(questionId);
		logger.debug(JSON.toJSONString(result));
		return result;
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/del/id", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "根据选项ID删除选项")
	public boolean deleteById(String id) {
		try {
			voteOptionRepository.deleteById(id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/del/qid", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "根据问题ID删除问题所有选项")
	public boolean deleteAllBySubjectId(String questionId) {
		try {
			voteOptionRepository.deleteAllByQuestionId(questionId);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
